<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\JsonRpc;

use App\Exception\BaseException;
use App\JsonRpc\Interfaces\GoodsServiceInterface;
use App\Repository\GoodsRepository;
use Hyperf\RpcServer\Annotation\RpcService;
use Hyperf\Di\Annotation\Inject;

/**
 * @RpcService(name="Goods::GoodsService", protocol="jsonrpc", server="jsonrpc")
 */
class GoodsService extends BaseService implements GoodsServiceInterface
{

    public function init()
    {
        $this->repository = GoodsRepository::getInstance();
    }

    /**
     * Notes: 商品初始项
     * @return array
     */
    public function initData():array
    {
        return $this->repository->initData();
    }

    /**
     * Notes: 默认添加
     * Author: Bruce.z
     * DateTime: 2021/12/22 20:17
     * @param array $data
     * @return \App\Model\BaseModel|\Hyperf\Database\Model\Model
     */
    public function add(array $data)
    {
        return $this->repository->baseCreate($data);
    }

    /**
     * Notes: 列表增加 根据产品关键字搜索
     * Author: Bruce.z
     * DateTime: 2022/6/6 17:15
     * @param array $condition
     * @param array $field
     * @param int $page
     * @param int $size
     * @return array
     */
    public function index(array $condition, array $field, int $page, int $size): array
    {
        if(isset($condition['name']) && !empty($condition['name'])){
            $condition[] = ['name', 'like', '%'.trim($condition['name']).'%'];
        }
        if(isset($condition['category_id']) && $condition['category_id'] == 0){
            unset($condition['category_id']);
        }
        unset($condition['name'] );

        return parent::index($condition, $field, $page, $size);
    }


    /**
     * Notes:更新商品信息
     * Author: Bruce.z
     * DateTime: 2022/6/6 17:31
     * @param int $id
     * @param array $data
     * @return bool
     * @throws BaseException
     */
    public function update(int $id, array $data): bool
    {
        return GoodsRepository::getInstance()->baseUpdate($id, $data);
    }


    /**
     * Notes: c端首页新品推荐
     * Author: Bruce.z
     * DateTime: 2022/6/13 17:53
     * @return array
     */
    public function appIndexList():array
    {
        return GoodsRepository::getInstance()->appIndexList();
    }

    /**
     * Notes: c端 商品列表
     * Author: Bruce.z
     * DateTime: 2022/6/14 15:33
     * @param array $param
     * @return mixed
     */
    public function appList(array $param)
    {
        return GoodsRepository::getInstance()->appList($param);
    }

    /**
     * Notes: c端 商品详情
     * Author: Bruce.z
     * DateTime: 2022/6/14 15:33
     * @param int $id
     * @param int $member_id
     * @return mixed
     */
    public function appDetail(int $id, int $member_id):array
    {
        return GoodsRepository::getInstance()->appDetail($id, $member_id);
    }

    /**
     * Notes: 返回货品库存
     * Author: Bruce.z
     * DateTime: 2022/6/27 16:43
     * @param array $data
     * @return int
     */
    public function getProductStock(array $data):int
    {
        return GoodsRepository::getInstance()->getProductStock($data);
    }

    /**
     * Notes: 返回商品状态
     * Author: Bruce.z
     * DateTime: 2022/6/30 17:27
     * @param array $ids
     * @return array
     */
    public function getStatusIds(array $ids):array
    {
        $res = GoodsRepository::getInstance()->getRows(['id'=>$ids], ['id','is_up','retail_price','stock','name','img']);
        return array_column($res, null,'id');
    }

}
